package cn.alcatraz.org.service.impl;

import cn.alcatraz.basic.service.impl.BaseServiceImpl;
import cn.alcatraz.org.domain.Department;
import cn.alcatraz.org.mapper.DepartmentMapper;
import cn.alcatraz.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * @Title: save
     * @Description: 添加一条部门表数据
     * @Author: TinyYu
     * @Version: 1.0
     * @Date: 2022/6/21 17:13
     * @Parameters:
     * @Return
     */
    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department);
        // 获取上级
        Department parent = departmentMapper.loadById(department.getParent().getId());
        String dirPath = "";
        if (parent != null) {
            // 拼接上级部门路径
            dirPath = parent.getDirPath() + "/" + department.getId();
        } else {
            dirPath = "/" + department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.updateById(department);
    }

    /**
     * @Title: updateById
     * @Description: 通过id修改部门表数据
     * @Author: TinyYu
     * @Version: 1.0
     * @Date: 2022/6/21 17:14
     * @Parameters:
     * @Return
     */
    @Override
    @Transactional
    public void updateById(Department department) {
        Long parentId = department.getParent().getId();
        // 获取上级
        Department parent = departmentMapper.loadById(parentId);
        String dirPath = "";
        if (parent != null) {
            // 拼接上级部门路径
            dirPath = parent.getDirPath() + "/" + department.getId();
        } else {
            dirPath = "/" + department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.updateById(department);
        // 获取下级
        List<Department> children = departmentMapper.getChildren(department.getId());
        if (children == null) {
            return;
        }
        for (Department child : children) {
            updateById(child);
        }

    }

    /**
    * @Title: tree
    * @Description: 获取无限极树
    * @Author: TinyYu
    * @Version: 1.0
    * @Date:  2022/6/27 14:35
    * @Parameters:
    * @Return
    */
    @Override
    public List<Department> tree() {
        // 获取部门全部数据
        List<Department> departments = departmentMapper.loadAll();

        // list转map
        Map<Long, Department> departmentMap = departments.stream().collect(Collectors.toMap(Department::getId, item -> item));

        // 存放返回数据信息
        List<Department> depts = new ArrayList<Department>();

        // 获取部门上级部门
        for (Department department : departments) {
            // 获取顶级部门
            if (department.getParent_id() == null) {
                depts.add(department);
            } else {
                Department dept = departmentMap.get(department.getParent_id());
                if (dept != null) {
                    dept.getChildren().add(department);
                }
            }
        }
        return depts;
    }
}
